package com.wang.commons.utils;

import com.wang.commons.exception.DiyException;
import com.wang.commons.pojo.StatusCode;

import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class DateUtil {
    private static String defaultFormat="yyyy-MM-dd HH:mm:ss";

    public static String parseDateLong(Long time, String format){
        SimpleDateFormat sd = new SimpleDateFormat(format);
        return sd.format(new Date(time));
    }

    public static Long parseDateString(String time,String format) throws ParseException {
        SimpleDateFormat sd=new SimpleDateFormat(format);
        return sd.parse(time).getTime();
    }

    //重载时间默认转换
    public static Long defaultParse(String obj){
        try {
            return parseDateString(obj, defaultFormat);
        } catch (ParseException e) {
            throw new DiyException(StatusCode.NORMALERROR.getCode(),"转换异常");
    }

    }
    public static String defaultParse(Long obj) {
        return parseDateLong( obj,defaultFormat);

    }

    public static void main(String[] args) throws Exception {
    System.out.println(defaultParse("2021-11-20 13:27:54"));
        System.out.println(defaultParse(System.currentTimeMillis()));
    }
}
